package com.clkj.sunhelp.mvp.seekhelpnew.detail;

import android.content.DialogInterface;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.os.Handler;
import android.os.Looper;
import android.os.Message;
import android.support.v7.app.AlertDialog;
import android.text.SpannableString;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import com.clkj.sunhelp.R;
import com.clkj.sunhelp.mvp.base.BaseActivity;
import com.clkj.sunhelp.mvp.seekhelpnew.QzrServices;
import com.clkj.sunhelp.mvp.seekhelpnew.WebserviceConfig;
import com.clkj.sunhelp.mvp.seekhelpnew.domain.Qz_viewItem;
import com.clkj.sunhelp.util.CommonUtil;
import com.clkj.sunhelp.util.Res;
import com.clkj.sunhelp.util.ToastUtil;
import com.clkj.sunhelp.util.viewutil.BitmapTransformation2;
import com.squareup.picasso.Picasso;

import java.util.HashMap;

/**
 * Created by Administrator on 2017/3/30.
 */

public class ActivitySeekHelpDetail extends BaseActivity{


    private TextView tvIWantJuanzhu;
    private TextView tvSeekHelpTime;
    private TextView tvHasJuanZengMoney;
    private TextView tvJuankuanWay;
    private TextView tvQiuzhuDesc;
    private TextView tvQiuzhuPerson;



    private ImageView ivZhengmingPic;

    private HashMap<String, Object> m_qzrinfo;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_seek_help_detail);
        initTitleWithRightTvOrIv(null,null,"求助详情",true,null,null);
        initData();
        assignView();
        initView();
        new Thread(getQzxxDetailThread).start();
    }




    @Override
    public void initData() {
        m_qzrinfo = (HashMap<String, Object>) (getIntent()
                .getSerializableExtra("m_qzrinfo"));
    }

    @Override
    public void assignView() {
        tvIWantJuanzhu = (TextView) findViewById(R.id.tv_i_want_juanzhu);
        tvSeekHelpTime = (TextView) findViewById(R.id.tv_seek_help_time);
        tvHasJuanZengMoney = (TextView) findViewById(R.id.tv_has_juan_zeng_money);
        tvJuankuanWay = (TextView) findViewById(R.id.tv_juankuan_way);
        tvQiuzhuDesc = (TextView) findViewById(R.id.tv_qiuzhu_desc);
        ivZhengmingPic = (ImageView) findViewById(R.id.iv_zhengming_pic);

        tvQiuzhuPerson = (TextView) findViewById(R.id.tv_qiuzhu_person);

    }

    @Override
    public void initView() {
        tvIWantJuanzhu.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                showToCiShanNetSeekHelpDialog();
            }
        });
    }


    private Runnable getQzxxDetailThread = new Runnable() {
        @Override
        public void run() {
            Looper.prepare();
            try {
                Qz_viewItem qzr = QzrServices.getYgttDetailById(m_qzrinfo.get(
                        "row_id").toString());
                // m_qzrinfo.put("QZXXID", qzr.getQZXXID());row_id
                m_qzrinfo.put("QZRNAME", qzr.getId().getqZRNAME());
                m_qzrinfo.put("CONTENT", qzr.getcONTENT());
                m_qzrinfo.put("QZTIME", qzr.getqZTIME());
                m_qzrinfo.put("HSSTATUS", qzr.gethSSTATUS());
                m_qzrinfo.put("QZTYPE", qzr.getqZTYPE());
                m_qzrinfo.put("DJS", Long.valueOf(qzr.getdJS()).longValue());
                m_qzrinfo.put("PLS", Long.valueOf(qzr.getpLS()).longValue());
                m_qzrinfo.put("YJKJE", Double.valueOf(qzr.getyJZJE())
                        .doubleValue());
                m_qzrinfo.put("PICBYTE", WebserviceConfig.HACIAIUPURL
                        + m_qzrinfo.get("imgurl").toString());
                m_qzrinfo.put("GZS", qzr.getgZS());
                m_qzrinfo.put("DYXS", qzr.getdYXS());
                m_qzrinfo.put("SCS", qzr.getsCS());
            } catch (Exception e) {
                e.printStackTrace();
            }
            Message msg = new Message();
            msg.what = 0;
            msg.obj = "";
            handler.sendMessage(msg);
        }
    };
    private Handler handler = new Handler() {
        @SuppressWarnings("unchecked")
        @Override
        public void handleMessage(Message msg) {
            if (msg.what == 0) {
                try {
                    String xxurl = (String) (m_qzrinfo.get("PICBYTE"));

                    Picasso.with(ActivitySeekHelpDetail.this)
                            .load(xxurl)
                            .placeholder(R.drawable.pic_default_trans)
                            .error(R.drawable.pic_default_trans)
                            .transform(new BitmapTransformation2(ActivitySeekHelpDetail.this,R.drawable.pic_default_trans))
                            .into(ivZhengmingPic);
                    tvQiuzhuPerson.setText("求助人：" + m_qzrinfo.get("QZRNAME"));
                    String qztimeStr = (String) m_qzrinfo.get("QZTIME");
                    tvSeekHelpTime.setText("求助时间：" + qztimeStr.substring(0, 11));
                    tvHasJuanZengMoney.setText("已捐赠金额：" + m_qzrinfo.get("YJKJE")+"元");
                    tvQiuzhuDesc.setText(String.valueOf(m_qzrinfo.get("CONTENT")));
                    /*gzBt.setText("关注(" + m_qzrinfo.get("GZS") + ")");
                    dyxBt.setText("顶一下(" + m_qzrinfo.get("DYXS") + ")");
                    scBt.setText("收藏(" + m_qzrinfo.get("SCS") + ")");
                    cGZS = (Integer.parseInt(m_qzrinfo.get("GZS").toString()));
                    cDYXS = (Integer.parseInt(m_qzrinfo.get("DYXS").toString()));
                    cSCS = Integer.parseInt(m_qzrinfo.get("SCS").toString());*/
                } catch (Exception e) {
                    //logger.error("初始化求助人信息错误：", e);
                    ToastUtil.showShort(ActivitySeekHelpDetail.this,"获取数据失败");
                }
            }
        }
    };

    private void showToCiShanNetSeekHelpDialog()
    {

        String originalStr = "请到淮安慈善网进行捐助操作，网址是：http://www.haciai.cn";

        SpannableString aimSpanStr = CommonUtil.getAimTextColorSpannableStr(originalStr, Res.getColor(R.color.graydark),Res.getColor(R.color.color_main),0,17);
        new AlertDialog.Builder(this).setTitle("提示")
                .setMessage(aimSpanStr)
                .setPositiveButton("确定", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        Intent intent= new Intent();
                        intent.setAction("android.intent.action.VIEW");
                        Uri content_url = Uri.parse("http://www.haciai.cn");
                        intent.setData(content_url);
                        startActivity(intent);
                    }
                }).setNegativeButton("取消",null).create().show();
    }
}
